package com.example.administrator.nl.util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;

import java.util.List;

public class ViewPageFragmentAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> fragments;
    private List<String> list;
    private FragmentManager mFragmentManager;
    public ViewPageFragmentAdapter(FragmentManager fm, List<Fragment> fragmenlist) {
        super(fm);
        this.mFragmentManager=fm;
        this.fragments = fragmenlist;
    }
    public ViewPageFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.mFragmentManager=fm;
    }

    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
        if(this.fragments != null){
            FragmentTransaction ft = mFragmentManager.beginTransaction();
            for(Fragment f:this.fragments){
                ft.remove(f);
            }
            ft.commit();
            ft=null;
            mFragmentManager.executePendingTransactions();
        }
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    public List<String> getList() {
        return list;
    }

    public ViewPageFragmentAdapter(FragmentManager fm, List<Fragment> fragmenlist, List<String> list) {
        super(fm);
        this.mFragmentManager=fm;
        this.fragments = fragmenlist;
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments == null ? 0 : fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list==null?super.getPageTitle(position):list.get(position);
    }
    public List<Fragment> getFragmenlist() {
        return fragments;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}